/*
 * @lc app=leetcode id=498 lang=cpp
 *
 * [498] Diagonal Traverse
 */

// @lc code=start
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
        int i = 0; int j = 0;
        vector<int> ans;
        int F = 1;
        int m = mat.size();
        int n = mat[i].size();
        while(true) {
            if (i >= 0 && i < m && j >= 0 && j < n) {
                ans.push_back(mat[i][j]);
            } 
            if (i == m - 1 && j == n -1) {
                break;
            }
            if (F){
                if (i==0) {
                    j++;
                    F = 0;
                } else {
                    i--;
                    j++;
                } 
            } else {
                if (j==0){
                    i++;
                    F = 1;
                } else {
                    i++;
                    j--;
                }
            }
        }
        return ans;
    }
};
// @lc code=end

